package cn.ronghuanet.org.service.impl;

import cn.ronghuanet.base.query.BaseQuery;
import cn.ronghuanet.base.service.impl.BaseServiceImpl;
import cn.ronghuanet.base.util.MD5Utils;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.mapper.EmployeeMapper;
import cn.ronghuanet.org.service.IEmployeeService;
import cn.ronghuanet.user.domain.Logininfo;
import cn.ronghuanet.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Transactional
    @Override
    public void saveEmployee(Employee employee) {
        // 1 新增登录信息到logininfo
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        // 1-用户  0-员工
        logininfo.setType(0);
        logininfo.setDisable(1);
        // 用户注册的时候,存储密码时需要对密码做加盐加密的处理
        String salt = UUID.randomUUID().toString();
        String password = MD5Utils.encrypByMd5("ronghua"+employee.getPassword()+"1234"+salt);
        logininfo.setSalt(salt);
        logininfo.setPassword(password);
        logininfoMapper.insert(logininfo);

        // 2 将loginingo的id设置给employee,再新增employee到数据库
        employee.setLogininfoId(logininfo.getId());
        employee.setSalt(salt);
        employee.setPassword(password);
        employeeMapper.insert(employee);
    }
}
